package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportUserCardRpcService;
import com.hunttown.mes.rpc.domain.TransportUserCardDTO;
import com.hunttown.mes.rpc.domain.query.TransportUserCardDTOQuery;
import com.hunttown.mes.service.TransportUserCardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportUserCardConverts.convert;

/**
 * created by wangjunfu on 2019-07-06 10:58:59
 */
@Service
public class TransportUserCardRpcServiceImpl implements TransportUserCardRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportUserCardRpcServiceImpl.class);

    private final TransportUserCardService transportUserCardService;

    @Autowired
    public TransportUserCardRpcServiceImpl(TransportUserCardService transportUserCardService) {
        this.transportUserCardService = transportUserCardService;
    }

    @Override
    public TransportUserCardDTO insert(TransportUserCardDTO transportUserCardDTO) {
        try {
            return convert(transportUserCardService.insert(convert(transportUserCardDTO)));
        } catch (Exception e) {
            logger.error("TransportUserCard create failed.", e);
            throw new RuntimeException("TransportUserCard create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportUserCardDTO transportUserCardDTO) {
        try {
            return transportUserCardService.updateInfoById(convert(transportUserCardDTO));
        } catch (Exception e) {
            logger.error("TransportUserCard update by id failed.", e);
            throw new RuntimeException("TransportUserCard update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportUserCardDTOQuery transportUserCardDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserCardDTOQuery.getW_id() != null && transportUserCardDTOQuery.getW_id() > 0) {
                query.put("w_id", transportUserCardDTOQuery.getW_id());
            }

            if (transportUserCardDTOQuery.getIds() != null && !transportUserCardDTOQuery.getIds().equals("")) {
                query.put("ids", transportUserCardDTOQuery.getIds());
            }

            if (transportUserCardDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCardDTOQuery.getStopFlag());
            }

            if (transportUserCardDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCardDTOQuery.getDeleteFlag());
            }

            return transportUserCardService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportUserCard update by query failed.", e);
            throw new RuntimeException("TransportUserCard update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCardDTO selectById(Integer id) {
        try {
            return convert(transportUserCardService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportUserCard select by id failed.", e);
            throw new RuntimeException("TransportUserCard select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportUserCardDTO selectObjByQuery(TransportUserCardDTOQuery transportUserCardDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportUserCardDTOQuery.getEntId() != null) {
                query.put("entId", transportUserCardDTOQuery.getEntId());
            }

            if (StringUtils.isNotBlank(transportUserCardDTOQuery.getCardNo())) {
                query.put("cardNo", transportUserCardDTOQuery.getCardNo());
            }

            if (transportUserCardDTOQuery.getUserId() != null && transportUserCardDTOQuery.getUserId() > 0) {
                query.put("userId", transportUserCardDTOQuery.getUserId());
            }

            if (transportUserCardDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportUserCardDTOQuery.getStopFlag());
            }

            if (transportUserCardDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportUserCardDTOQuery.getDeleteFlag());
            }

            return convert(transportUserCardService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportUserCard select obj by query failed.", e);
            throw new RuntimeException("TransportUserCard select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportUserCardDTO> getListForPage(TransportUserCardDTOQuery transportUserCardDTOQuery) {
        Query query = Query.page(transportUserCardDTOQuery.getPn(), transportUserCardDTOQuery.getPageSize());

        if (transportUserCardDTOQuery.getId() != null && transportUserCardDTOQuery.getId() > 0) {
            query.put("id", transportUserCardDTOQuery.getId());
        }

        if (transportUserCardDTOQuery.getIds() != null && !transportUserCardDTOQuery.getIds().equals("")) {
            query.put("ids", transportUserCardDTOQuery.getIds());
        }

        if (transportUserCardDTOQuery.getUserId() != null && transportUserCardDTOQuery.getUserId() > 0) {
            query.put("userId", transportUserCardDTOQuery.getUserId());
        }

        if (StringUtils.isNotBlank(transportUserCardDTOQuery.getCardNo())) {
            query.put("cardNo", transportUserCardDTOQuery.getCardNo());
        }

        if (StringUtils.isNotBlank(transportUserCardDTOQuery.getLikeName())) {
            query.put("likeName", transportUserCardDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(transportUserCardDTOQuery.getShowDate())) {
            query.put("showDate", transportUserCardDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportUserCardDTOQuery.getStartDate())) {
            query.put("startDate", transportUserCardDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportUserCardDTOQuery.getEndDate())) {
            query.put("endDate", transportUserCardDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportUserCardDTOQuery.getOrderBy())) {
            query.put("orderBy", transportUserCardDTOQuery.getOrderBy());
        }

        if (transportUserCardDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportUserCardDTOQuery.getStopFlag());
        }

        if (transportUserCardDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportUserCardDTOQuery.getDeleteFlag());
        }

        List<TransportUserCardDTO> list = convert(transportUserCardService.selectByQuery(query));
        Page<TransportUserCardDTO> page = Page.from(transportUserCardDTOQuery.getPn(), transportUserCardDTOQuery.getPageSize(), list);
        page.setTotalItems(transportUserCardService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportUserCardService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportUserCard delete failed.", e);
            throw new RuntimeException("TransportUserCard delete failed, Please order your code.", e);
        }
    }
}